package com.voute.Presenter.Impl;

import com.voute.Model.Bean.BaseBean;
import com.voute.Model.Bean.FeedDetailBean;
import com.voute.Model.Bean.UxTagBean;
import com.voute.Model.Bean.VoteResultBean;
import com.voute.Model.IFeedDetailModel;
import com.voute.Model.IUxTagModel;
import com.voute.Model.Impl.FeedDetailModelImpl;
import com.voute.Model.Impl.UXTagModelImpl;
import com.voute.Presenter.IFeedDetailPresenter;
import com.voute.Utils.CommonSubscriber;
import com.voute.Utils.TransformUtils;
import com.voute.View.IView.IFeedDetailView;

import java.util.HashMap;

import rx.Observable;
import rx.functions.Action0;
import rx.functions.Func1;

/**
 * feed详情的presenter实现类
 * Created by liucxu on 2017/1/23.
 */

public class FeedDetailPresenterImpl implements IFeedDetailPresenter {

    private IFeedDetailView detail_view;
    private IFeedDetailModel model;
    private IUxTagModel tagModel;

    public FeedDetailPresenterImpl(IFeedDetailView view) {
        detail_view = view;
        model = new FeedDetailModelImpl();
        tagModel = new UXTagModelImpl();
    }

    @Override
    public void getFeedDetails(String fid) {
        detail_view.showLoading();
        model.getFeedDetails(fid).compose(TransformUtils.<FeedDetailBean>defaultSchedulers()).subscribe(new CommonSubscriber<FeedDetailBean>() {
            @Override
            public void onSuccess(FeedDetailBean response) {
                detail_view.hideLoading();
                detail_view.showFeedDetail(response);
            }

            @Override
            public void onFailure(BaseBean response) {
                detail_view.hideLoading();
                detail_view.showError(response.getMsg());
            }
        });
    }

    @Override
    public void starFeed(final String fid) {
        model.starFeeds(fid).compose(TransformUtils.<BaseBean>defaultSchedulers()).subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {
                detail_view.starSuccess();
            }

            @Override
            public void onFailure(BaseBean response) {
                detail_view.showError(response.getMsg());
            }
        });
    }

    @Override
    public void voteFeed(final String fid, final String count, final String side) {
        detail_view.showLoading();
        tagModel.getUxTag().flatMap(new Func1<UxTagBean, Observable<VoteResultBean>>() {
            @Override
            public Observable<VoteResultBean> call(UxTagBean uxTagBean) {
                HashMap<String, String> values = new HashMap<String, String>();
                values.put("count", count);
                values.put("side", side);
                values.put("uxtag", uxTagBean.getData().getUxtag());
                return model.voteFeed(fid, values);
            }
        }).compose(TransformUtils.<VoteResultBean>defaultSchedulers()).subscribe(new CommonSubscriber<VoteResultBean>() {
            @Override
            public void onSuccess(VoteResultBean response) {
                detail_view.hideLoading();
                detail_view.voteSuccess(response);
            }

            @Override
            public void onFailure(BaseBean response) {
                detail_view.hideLoading();
                detail_view.showError(response.getMsg());
            }
        });
    }

    @Override
    public void unStarFeed(final String fid) {
        model.unstarFeeds(fid).compose(TransformUtils.<BaseBean>defaultSchedulers()).subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {
                detail_view.unstarSuccess();
            }

            @Override
            public void onFailure(BaseBean response) {
                detail_view.showError(response.getMsg());
            }
        });
    }

    @Override
    public void upComment(final String cid, final int is_pos) {
        tagModel.getUxTag().flatMap(new Func1<UxTagBean, Observable<BaseBean>>() {
            @Override
            public Observable<BaseBean> call(UxTagBean uxTagBean) {
                return model.upComment(cid, uxTagBean.getData().getUxtag(), is_pos);
            }
        }).compose(TransformUtils.<BaseBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {
                detail_view.upCommentSuccess();
            }

            @Override
            public void onFailure(BaseBean response) {
                detail_view.upCommentFailure(response);
            }
        });
    }

    @Override
    public void reportFeed(final String fid, final String reportReson) {
        tagModel.getUxTag().flatMap(new Func1<UxTagBean, Observable<BaseBean>>() {
            @Override
            public Observable<BaseBean> call(UxTagBean uxTagBean) {
                HashMap<String,String> params=new HashMap<String, String>();
                params.put("fid",fid);
                params.put("reason",reportReson);
                params.put("uxtag",uxTagBean.getData().getUxtag());
                return model.reportFeed(fid,params);
            }
        }).compose(TransformUtils.<BaseBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                detail_view.showLoading();
            }
        }).subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {
                detail_view.hideLoading();
                detail_view.reportSuccess();
            }

            @Override
            public void onFailure(BaseBean response) {
                detail_view.hideLoading();
                detail_view.reportFailure(response.getMsg());
            }
        });
    }
}
